package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName AddressBookController
 * @Description C端-地址簿管理
 * @Author sld
 * @Date 2024/7/12 15:36
 * Version 1.0
 **/
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿管理")
@Slf4j
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;

    @PostMapping
    @ApiOperation("新增地址")
    public Result add(@RequestBody AddressBook addressBook){
        log.info("新增地址：{}",addressBook);
        // 调用业务层
        addressBookService.add(addressBook);
        // 返回结果
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询用户的所有地址")
    public Result<List<AddressBook>> list(){
        log.info("查询用户的所有地址");
        // 调用业务层
        List<AddressBook> list = addressBookService.list();
        // 返回结果
        return Result.success(list);
    }

    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址：{}",addressBook);
        // 调用业务层
        addressBookService.setDefault(addressBook);
        // 返回结果
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改地址")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址：{}",addressBook);
        // 调用业务层
        addressBookService.update(addressBook);
        // 返回结果
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable("id") Long id){
        log.info("根据id查询地址：{}",id);
        // 调用业务层
        AddressBook addressBook = addressBookService.getById(id);
        // 返回结果
        return Result.success(addressBook);
    }
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefault(){
        log.info("查询默认地址");
        // 调用业务层
        AddressBook addressBook = addressBookService.getDefault();
        // 返回结果
        return Result.success(addressBook);
    }
}
